home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 53 / PC Actual CD 53.iso / VNULabs / ProgramacionSonido / WTest / WTest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-07  |  5.7 KB  |  231 lines

  1. // WTest.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include <objbase.h>
  5. #include <initguid.h>
  6. #include <commdlg.h>
  7. #include <mmreg.h>
  8. #include <dsound.h>
  9. #include "resource.h"
  10. #include "DSUtiles.h"
  11.  
  12.  
  13. #define MAX_LOADSTRING 100
  14.  
  15. // Global Variables:
  16. HINSTANCE hInst;                     // current instance
  17. TCHAR szTitle[MAX_LOADSTRING];         // The title bar text
  18. TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
  19. //
  20. // Mas Globales
  21. //
  22. HWND                hWnd;
  23. CDSUtil             DSUtil;
  24.  
  25. // Foward declarations of functions included in this code module:
  26. ATOM                MyRegisterClass(HINSTANCE hInstance);
  27. BOOL                InitInstance(HINSTANCE, int);
  28. LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
  29. LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
  30.  
  31. int APIENTRY WinMain(HINSTANCE hInstance,
  32.                      HINSTANCE hPrevInstance,
  33.                      LPSTR     lpCmdLine,
  34.                      int       nCmdShow)
  35. {
  36.      // TODO: Place code here.
  37.     MSG    msg;
  38.     HACCEL hAccelTable;
  39.     BOOL   Done;
  40.  
  41.     // Initialize global strings
  42.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  43.     LoadString(hInstance, IDC_WTEST, szWindowClass, MAX_LOADSTRING);
  44.     MyRegisterClass(hInstance);
  45.  
  46.     // Perform application initialization:
  47.     if (!InitInstance (hInstance, nCmdShow)) 
  48.     {
  49.         return FALSE;
  50.     }
  51.  
  52.     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WTEST);
  53.  
  54.    //
  55.    // Inicializamos el sistema de sonido
  56.    // y cargamos el sonido en el buffer.
  57.    //
  58.    if (!DSUtil.Init (hWnd,hInst,NULL))
  59.    {
  60.       MessageBox(hWnd,"No se pudo inicializar DirectSound","Error",MB_OK | MB_ICONSTOP);
  61.       return FALSE;
  62.    }
  63.    if (!DSUtil.LoadWavInBuffer ("sonido.wav"))
  64.    {
  65.      MessageBox(hWnd,"No se pudo inicializar el buffer","Error",MB_OK | MB_ICONSTOP);
  66.      return FALSE;
  67.    }
  68.  
  69.   // El bucle principal.
  70.    Done = FALSE;
  71.    while ( !Done )
  72.    {
  73.      while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
  74.      {
  75.        if (msg.message==WM_QUIT) Done = TRUE;
  76.        else
  77.        {
  78.          TranslateMessage( &msg );
  79.          DispatchMessage( &msg );
  80.        }
  81.      }
  82.      if (DSUtil.bPlaying) DSUtil.Play();
  83.    }
  84.    return msg.wParam;
  85. }
  86.  
  87.  
  88.  
  89. //
  90. //  FUNCTION: MyRegisterClass()
  91. //
  92. //  PURPOSE: Registers the window class.
  93. //
  94. //  COMMENTS:
  95. //
  96. //    This function and its usage is only necessary if you want this code
  97. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  98. //    function that was added to Windows 95. It is important to call this function
  99. //    so that the application will get 'well formed' small icons associated
  100. //    with it.
  101. //
  102. ATOM MyRegisterClass(HINSTANCE hInstance)
  103. {
  104.     WNDCLASSEX wcex;
  105.  
  106.     wcex.cbSize = sizeof(WNDCLASSEX); 
  107.  
  108.     wcex.style            = CS_HREDRAW | CS_VREDRAW;
  109.     wcex.lpfnWndProc    = (WNDPROC)WndProc;
  110.     wcex.cbClsExtra        = 0;
  111.     wcex.cbWndExtra        = 0;
  112.     wcex.hInstance        = hInstance;
  113.     wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_WTEST);
  114.     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
  115.     wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
  116.     wcex.lpszMenuName    = (LPCSTR)IDC_WTEST;
  117.     wcex.lpszClassName    = szWindowClass;
  118.     wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  119.  
  120.     return RegisterClassEx(&wcex);
  121. }
  122.  
  123. //
  124. //   FUNCTION: InitInstance(HANDLE, int)
  125. //
  126. //   PURPOSE: Saves instance handle and creates main window
  127. //
  128. //   COMMENTS:
  129. //
  130. //        In this function, we save the instance handle in a global variable and
  131. //        create and display the main program window.
  132. //
  133. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  134. {
  135.    hInst = hInstance; // Store instance handle in our global variable
  136.  
  137.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  138.       CW_USEDEFAULT, CW_USEDEFAULT, 320,46, NULL, NULL, hInstance, NULL);
  139.  
  140.    if (!hWnd)
  141.    {
  142.       return FALSE;
  143.    }
  144.  
  145.    ShowWindow(hWnd, nCmdShow);
  146.    UpdateWindow(hWnd);
  147.  
  148.    return TRUE;
  149. }
  150.  
  151. //
  152. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  153. //
  154. //  PURPOSE:  Processes messages for the main window.
  155. //
  156. //  WM_COMMAND    - process the application menu
  157. //  WM_PAINT    - Paint the main window
  158. //  WM_DESTROY    - post a quit message and return
  159. //
  160. //
  161. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  162. {
  163.     int wmId, wmEvent;
  164.     PAINTSTRUCT ps;
  165.     HDC hdc;
  166.     TCHAR szHello[MAX_LOADSTRING];
  167.     LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
  168.  
  169.     switch (message) 
  170.     {
  171.         case WM_COMMAND:
  172.             wmId    = LOWORD(wParam); 
  173.             wmEvent = HIWORD(wParam); 
  174.             // Parse the menu selections:
  175.             switch (wmId)
  176.             {
  177.                 case ID_SOUND_PLAY:
  178.                    DSUtil.StartPlay();
  179.                    break;
  180.                 case ID_SOUND_STOP:
  181.                    DSUtil.StopPlay();
  182.                    break;
  183.                 case IDM_ABOUT:
  184.                    DSUtil.StopPlay();
  185.                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  186.                    DSUtil.StartPlay();
  187.                    break;
  188.                 case IDM_EXIT:
  189.                    DestroyWindow(hWnd);
  190.                    break;
  191.                 default:
  192.                    return DefWindowProc(hWnd, message, wParam, lParam);
  193.             }
  194.             break;
  195.         case WM_PAINT:
  196.             hdc = BeginPaint(hWnd, &ps);
  197.             // TODO: Add any drawing code here...
  198.             RECT rt;
  199.             GetClientRect(hWnd, &rt);
  200.             DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
  201.             EndPaint(hWnd, &ps);
  202.             break;
  203.         case WM_DESTROY:
  204.             DSUtil.End();
  205.             PostQuitMessage(0);
  206.             break;
  207.         default:
  208.             return DefWindowProc(hWnd, message, wParam, lParam);
  209.    }
  210.    return 0;
  211. }
  212.  
  213. // Mesage handler for about box.
  214. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  215. {
  216.     switch (message)
  217.     {
  218.         case WM_INITDIALOG:
  219.                 return TRUE;
  220.  
  221.         case WM_COMMAND:
  222.             if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  223.             {
  224.                 EndDialog(hDlg, LOWORD(wParam));
  225.                 return TRUE;
  226.             }
  227.             break;
  228.     }
  229.     return FALSE;
  230. }
  231.